package StudentSystem;

import StudentSystem.Mthod.StudentMethod;
import StudentSystem.Mthod.UserMethod;
import StudentSystem.bean.Student;
import StudentSystem.bean.User;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystemTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<User> users = new ArrayList<>();
        // 登录界面
        whileLoop:
        while (true) {
            UserMethod.readFile(users);
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1：登录");
            System.out.println("请选择操作2：注册");
            System.out.println("请选择操作3：忘记密码");
            System.out.println("请选择操作0：退出");
            switchLoop:
            switch (sc.next()) {
                case "1" -> {
                    for (int i = 2; i >= 0; i--) {
                        System.out.println("请输入用户名：");
                        String username = sc.next();
                        System.out.println("请输入密码：");
                        String password = sc.next();
                        String code = UserMethod.checkCode();
                        System.out.println("请输入" + code + "验证码：");
                        String checkCode = sc.next();
                        if (users.size() == 0){
                            System.out.println("该用户不存在，请前去注册！！");
                            break switchLoop;
                        }
                        for (User user : users) {
                            if (!(code.equalsIgnoreCase(checkCode))) {
                                System.out.println("验证码错误，请重新输入！！您还剩下" + i + "次机会");
                                break;
                            } else if (!(username.equals(user.getUsername()) && password.equals(user.getPassword()))) {
                                System.out.println("用户名或密码错误，请重新输入！！您还剩下" + i + "次机会");
                                break;
                            } else {
                                break whileLoop;
                            }
                        }
                    }
                    System.out.println("您的机会已用光！！目前已锁定请耐心等待几分钟后重试");
                    System.exit(0);
                }
                case "2" -> UserMethod.register(users);
                case "3" -> UserMethod.replace(users);
                case "0" -> System.exit(0);
                default -> System.out.println("您输入的选项不存在请重新输入");
            }
        }

        //         读出文件
//         创建用于存储的文件对象
        ArrayList<Student> students = new ArrayList<>();
        while (true) {
            StudentMethod.readFile(students);
            System.out.println("-------------欢迎来到黑马学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择：");
            String select = sc.next();
            switch (select) {
                case "1" -> StudentMethod.add(students);
                case "2" -> StudentMethod.delete(students);
                case "3" -> StudentMethod.replace(students);
                case "4" -> StudentMethod.printStudent(students);
                case "5" -> System.exit(0);
                default -> {
                    System.out.println("您输入的选项有误，请重新输入！！");
                    students.clear();
                }
            }
        }
    }
}



